﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Web;
using ELGGAPI.Models;
using MySql.Data.MySqlClient;
    namespace ELGGAPI.Data
    {
        public class AlbumDetailModels
        {
            public int albumid { get; set; }
            public string title { get; set; }
            public string description { get; set; }
            public int? accessid { get; set; }
        }

        public class AlbumTagsModels
        {
            public int albumid { get; set; }
            public long? createdat { get; set; }
            public string tag { get; set; }
        }
        public class AlbumModels
        {
            public IEnumerable<AlbumDetailModels> albums { get; set; }
            public IEnumerable<AlbumTagsModels> albumtags { get; set; }
        }
        public class ImageDetailModels
        {
            public int imageid { get; set; }
            public string title { get; set; }
            public string description { get; set; }
            public int? accessid { get; set; }
            public string materimage { get; set; }
            public string thumbimage { get; set; }
            public string smallimage  { get; set; }
        }

        public class ImageTagsModels
        {
            public int imageid { get; set; }
            public long? createdat { get; set; }
            public string tag { get; set; }
        }
        public class ImageModels
        {
            public IEnumerable<ImageDetailModels> images { get; set; }
            public IEnumerable<ImageTagsModels> imagetags { get; set; }
        }
        public class AlbumDomainModels
        {
            public static AlbumModels GetAlbumList(int? pElggUserID)
            {
                AlbumModels album = new AlbumModels();
                DataSet dataSet = new DataSet();
                using (MySqlConnection connection = new MySqlConnection(APIHelper.ConnectionString))
                {
                    using (MySqlCommand selectCommand = new MySqlCommand(APIHelper.DBName + "my_getAlbumList", connection))
                    {
                        if (pElggUserID == 0)
                            selectCommand.Parameters.AddWithValue("pElggUserID", DBNull.Value);
                        else
                            selectCommand.Parameters.AddWithValue("pElggUserID", pElggUserID);
                        selectCommand.CommandType = CommandType.StoredProcedure;
                        using (MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter(selectCommand))
                            ((DataAdapter)mySqlDataAdapter).Fill(dataSet);
                    }
                }
                List<AlbumDetailModels> LstAlbumDetails = new List<AlbumDetailModels>();
                List<AlbumTagsModels> LstAlbumTags = new List<AlbumTagsModels>();
                if (dataSet.Tables.Count > 0)
                {
                    foreach (DataRow dataRow in (InternalDataCollectionBase)dataSet.Tables[0].Rows)
                    LstAlbumDetails.Add(new AlbumDetailModels()
                    {
                        albumid = int.Parse("0"+dataRow["AlbumGUID"].ToString()),
                        title = dataRow["AlbumTitle"].ToString(),
                        description = dataRow["AlbumDescription"].ToString(),
                        accessid = int.Parse(dataRow["Album_access_id"].ToString())
                    });
                }
                if (dataSet.Tables.Count > 1)
                {
                    foreach (DataRow dataRow in (InternalDataCollectionBase)dataSet.Tables[1].Rows)
                    LstAlbumTags.Add(new AlbumTagsModels()
                    {
                        albumid = int.Parse("0" + dataRow["AlbumGUID"].ToString()),
                        createdat = long.Parse(dataRow["time_created"].ToString()),
                        tag = dataRow["value"].ToString()
                    });
                }

                album.albumtags = LstAlbumTags;
                album.albums = LstAlbumDetails;
                return album;
            }

            public static ImageModels GetImageList(int? AlbumId)
            {
                ImageModels images = new ImageModels();
                DataSet dataSet = new DataSet();
                using (MySqlConnection connection = new MySqlConnection(APIHelper.ConnectionString))
                {
                    using (MySqlCommand selectCommand = new MySqlCommand(APIHelper.DBName + "my_getImageList", connection))
                    {
                        if (AlbumId == 0)
                            selectCommand.Parameters.AddWithValue("pAlbumGUID", DBNull.Value);
                        else
                            selectCommand.Parameters.AddWithValue("pAlbumGUID", AlbumId);
                        selectCommand.CommandType = CommandType.StoredProcedure;
                        using (MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter(selectCommand))
                            ((DataAdapter)mySqlDataAdapter).Fill(dataSet);
                    }
                }
                List<ImageDetailModels> LstImageDetails = new List<ImageDetailModels>();
                List<ImageTagsModels> LstImageTags = new List<ImageTagsModels>();

                if (dataSet.Tables.Count > 2)
                {
                    string baseurl = string.Empty;
                    foreach (DataRow dataRow in (InternalDataCollectionBase)dataSet.Tables[2].Rows)
                    {
                        baseurl = dataRow["url"].ToString();
                    }
                    foreach (DataRow dataRow in (InternalDataCollectionBase)dataSet.Tables[0].Rows)
                        LstImageDetails.Add(new ImageDetailModels()
                        {
                            imageid = int.Parse("0"+ dataRow["ImageGUID"].ToString()),
                            title = dataRow["ImageTitle"].ToString(),
                            description = dataRow["ImageDescription"].ToString(),
                            accessid = int.Parse(dataRow["Access_id"].ToString()),
                            materimage = baseurl + "photos/thumbnail/" + dataRow["ImageGUID"].ToString() + "/master/",
                            smallimage = baseurl + "photos/thumbnail/" + dataRow["ImageGUID"].ToString() + "/small/",
                            thumbimage = baseurl + "photos/thumbnail/" + dataRow["ImageGUID"].ToString() + "/thumb/"
                            
                        });
                    foreach (DataRow dataRow in (InternalDataCollectionBase)dataSet.Tables[1].Rows)
                        LstImageTags.Add(new ImageTagsModels()
                        {
                            imageid = int.Parse("0" + dataRow["ImageGUID"].ToString()),
                            createdat = long.Parse(dataRow["time_created"].ToString()),
                            tag = dataRow["value"].ToString()
                        });
                
                }
                
                images.imagetags = LstImageTags;
                images.images = LstImageDetails;
                return images;
            }
        }
    }
